<template>
  <el-dialog v-model="drawerVisible" :destroy-on-close="true" size="350px" :title="`${drawerProps.title}商品`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="问题反馈" prop="content">
        <div>{{ drawerProps.row!.content }}</div>
      </el-form-item>
      <el-form-item label="答复反馈" prop="response">
        <el-input
          v-model="drawerProps.row!.response"
          type="textarea"
          style="height: 60px"
          placeholder="请填写库存数量数"
        ></el-input>
      </el-form-item>
      <el-form-item label="处理状态" prop="status">
        <el-radio v-model="drawerProps.row!.status" :label="0">未处理</el-radio>
        <el-radio v-model="drawerProps.row!.status" :label="1">处理中</el-radio>
        <el-radio v-model="drawerProps.row!.status" :label="2">已处理</el-radio>
      </el-form-item>
      <el-form-item label="反馈图片" prop="imageUrls">
        <div class="img-preview-list">
          <el-image
            v-for="(url, index) in drawerProps.row!.imageUrls"
            :key="index"
            :src="url"
            :preview-src-list="drawerProps.row!.imageUrls"
            :preview-teleported="true"
            fit="cover"
            style="width: 80px; height: 80px; margin-right: 10px; border-radius: 4px"
          />
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
// import { genderType } from "@/utils/dict";
import { ElMessage, FormInstance } from "element-plus";
import { Feedback } from "@/api/interface";

const rules = reactive({});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<Feedback.ResFeedbackList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    const dataToSend = { ...drawerProps.value.row };
    const dataFrom = {
      feedbackId: dataToSend.id,
      adminId: dataToSend.adminId,
      response: dataToSend.response,
      status: dataToSend.status
    };
    console.log("提交的信息：", dataFrom);
    try {
      await drawerProps.value.api?.(dataFrom);
      ElMessage.success({ message: `${drawerProps.value.title}反馈成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
